package com.showgood.model {
	import com.adobe.serialization.json.JSON;
	import com.showgood.core.AbstractProxy;
	import com.showgood.config.Config;
	import com.showgood.config.SangoConst;
	import com.showgood.util.MapElem;
	import com.showgood.util.Path2I;
	import com.showgood.util.Rect;
	import com.showgood.util.GameUtil;
	import com.showgood.view.component.MarchPath;
	import com.showgood.view.mediator.ChuZhengDialogMediator;
	import com.showgood.view.mediator.CityDialogMediator;
	import com.showgood.vo.ArmySetupVO;
	import com.showgood.vo.ArmyVO;
	import com.showgood.vo.CityVO;
	import com.showgood.vo.LocationVO;
	import com.showgood.vo.RectVO;
	import com.showgood.vo.RemoteMessage;
	import com.showgood.view.GameView;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import com.showgood.view.CityDialog;
	import com.showgood.view.ChuZhengDialog;
	
	/**
	 * 地图场景的服务
	 * 1. 地图的刷新机制
	 * 2. 创建部队
	 * @author bianpeng
	 */
	public class GameViewProxy extends AbstractProxy {
		// 创建部队用的
		private var _createArmy:ArmyVO;
		private var _createArmyElem:MapElem;
		private var _marchPath:MarchPath;
		private var _setupInfo:ArmySetupVO;
		
		public function get createArmy():ArmyVO {
			return _createArmy;
		}
		private var _gameView:GameView;
		public function get gameView():GameView {
			return _gameView;
		}
		public function GameViewProxy(gameView:GameView) {
			_gameView = gameView;
			super(null);
		}
		public function queryMapByRect(x:int, y:int, w:int, h:int):void {
			var _socket:SocketProxy = sangoFacade.socket;
			// 构造一个矩形出来
			var vo:RectVO = new RectVO();
			vo.x = x;
			vo.y = y;
			vo.w = w;
			vo.h = h;
			_socket.sendSingleVo(RemoteMessage.REMOTE_QUERY_CITY_ON_MAP, 0, vo);
			_socket.sendSingleVo(RemoteMessage.REMOTE_QUERY_ARMY_ON_MAP, 0, vo);
		}
		
		/**
		 * 构造两个矩形, 分别向服务器请求数据
		 * 左上角为left, top
		 * 
		 * @param	left
		 * @param	top
		 * @param	dx
		 * @param	dy
		 */
		public function queryMapByPan(left:int, top:int, dx:int, dy:int):void {
			var _socket:SocketProxy = socketProxy;
			
			var _w:int = Config.SCREEN_WIDTH;
			var _h:int = Config.SCREEN_HEIGHT;
			// split screen in three rect
			var r1:Rect, r2:Rect, r3:Rect;
			
			if (dy >= 0) {
				r1 = new Rect(left, top, _w, dy);
				if (dx >= 0) {
					r2 = new Rect(left, top + dy, dx, _h - dy);
					r3 = new Rect(left + dx, top + dy, _w - dx, _h - dy);
				}
				else if (dx < 0) {
					dx = dx + _w;
					r2 = new Rect(left + dx, top + dy, _w - dx, _h - dy);
					r3 = new Rect(left, top + dy, dx, _h - dy);
				}
			}
			else if (dy < 0) {
				dy = dy + _h;
				r1 = new Rect(left, top+dy, _w, _h-dy);
				if (dx >= 0) {
					r2 = new Rect(left, top, dx, dy);
					r3 = new Rect(left+dx, top, _w-dx, dy);
				}
				else if (dx < 0) {
					dx = dx + _w;
					r2 = new Rect(left+dx, top, _w-dx, dy);
					r3 = new Rect(left, top, dx, dy);
				}
			}
			// 发请求在这里
			if (r1 != null && r1._w > 0 && r1._h > 0) {
				var vo1:RectVO = GameUtil.createRectVO2(r1);
				_logger.info(JSON.encode(vo1))
				_socket.sendSingleVo(RemoteMessage.REMOTE_QUERY_CITY_ON_MAP, 0, vo1);
				_socket.sendSingleVo(RemoteMessage.REMOTE_QUERY_ARMY_ON_MAP, 0, vo1);
			}
			if (r2 != null && r2._w > 0 && r2._h > 0) {
				var vo2:RectVO = GameUtil.createRectVO2(r2);
				_logger.info(JSON.encode(vo2))
				_socket.sendSingleVo(RemoteMessage.REMOTE_QUERY_CITY_ON_MAP, 0, vo2);
				_socket.sendSingleVo(RemoteMessage.REMOTE_QUERY_ARMY_ON_MAP, 0, vo2);
			}
			if (r3 != null && r3._w > 0 && r3._h > 0) {
				this.sendNotification(SangoConst.FILTER_CITY_ON_SCREEN, r3);
				// this.sendNotification(SangoConst.REMOTE_QUERY_ARMY_ON_MAP, r3);
			}
			
			// 绘制一下, 便于验证
			/*
			g.clear();
			fillRect(r1, 0xff0000, 0.2, left, top, g);
			fillRect(r2, 0x00ff00, 0.2, left, top, g);
			fillRect(r3, 0x0000ff, 0.2, left, top, g);
			*/
		}
		private function fillRect(r:Rect, c:int, a:Number, left:int, top:int):void {
			if (!r) return ;
			
			var g:Graphics = gameView.graphics;
			
			g.beginFill(c, a);
			g.lineStyle(1, c);
			g.drawRect(r._x - left, r._y - top, r._w, r._h);
			g.endFill();
		}
		/**
		 * 创建部队, 带上位置信息, 以及布阵信息...
		 * @param	x
		 * @param	y
		 */
		public function prepareArmy():void {
			var _src:CityVO = (sangoFacade.retrieveMediatorByType(CityDialogMediator).getViewComponent() as CityDialog).city;
			// TODO: info from chuzhengdialog 
			_createArmy = new ArmyVO();
			var _p:PreloadProxy = sangoFacade.retrieveProxyByType(PreloadProxy) as PreloadProxy;
			var _clazz:Class = _p.getClass('Army');
			var _it:DisplayObject = new _clazz();
			GameUtil.center(_it);
			_createArmyElem = new MapElem(_createArmy, _it);
			_createArmyElem.alpha = 0.7;
			_createArmyElem.x = GameUtil.calc_x(_src.x, _src.y);
			_createArmyElem.y = GameUtil.calc_y(_src.x, _src.y);
			gameView.armyLayer.addChild(_createArmyElem);
			_marchPath = new MarchPath();
			gameView.armyLayer.addChild(_marchPath);
		}
		/**
		 * 
		 * @param	done false => cancel, true => ok
		 */
		public function finishCreateArmy(done:Boolean):void {
			GameUtil.assert('not creating army', _createArmyElem != null);
			
			// 清理掉
			gameView.armyLayer.removeChild(_createArmyElem);
			gameView.armyLayer.removeChild(_marchPath);
			_createArmy = null;
			
			// 这里通知服务器, 我们创建了一个队伍
			if (done) {
				sangoFacade.socket.sendSingleVo(RemoteMessage.REMOTE_QUERY_ARMY_SETUP, _setupInfo.start, _setupInfo);
			}
		}
		public function get isCreateArmyMode():Boolean {
			return _createArmy != null;
		}
		/**
		 * 设置行军终点
		 */
		public function updateMarchPathToCity(_src:CityVO, _dst:CityVO):void {
			_marchPath.path = new Path2I(
				GameUtil.calc_x(_src.x, _src.y), GameUtil.calc_y(_src.x, _src.y),
				GameUtil.calc_x(_src.x, _dst.y), GameUtil.calc_y(_src.x, _dst.y),
				GameUtil.calc_x(_dst.x, _dst.y), GameUtil.calc_y(_dst.x, _dst.y)
				);
			var dlg: ChuZhengDialog = sangoFacade.retrieveMediatorByType(ChuZhengDialogMediator).getViewComponent() as ChuZhengDialog;
			
			_setupInfo = new ArmySetupVO();
			_setupInfo.type = 0; /// see, Army.Defs.TYPE_*
			_setupInfo.start = _src.id;
			_setupInfo.dest = _dst.id;
			_setupInfo.lineup = dlg.lineupId;
			_setupInfo.army = dlg.onLineupMan;
			_setupInfo.amount = dlg.amount;
			_setupInfo.type = SangoConst.ARMY_TYPE_CITY;
		}
		public function updateMarchPathToArmy(_src:CityVO, _dst:ArmyVO):void {
			_marchPath.path = new Path2I(
				GameUtil.calc_x(_src.x, _src.y), GameUtil.calc_y(_src.x, _src.y),
				GameUtil.calc_x(_src.x, _dst.y), GameUtil.calc_y(_src.x, _dst.y),
				GameUtil.calc_x(_dst.x, _dst.y), GameUtil.calc_y(_dst.x, _dst.y)
				);
			var dlg: ChuZhengDialog = sangoFacade.retrieveMediatorByType(ChuZhengDialogMediator).getViewComponent() as ChuZhengDialog;
			
			_setupInfo = new ArmySetupVO();
			_setupInfo.type = 0; /// see, Army.Defs.TYPE_*
			_setupInfo.start = _src.id;
			_setupInfo.dest = _dst.captain;
			_setupInfo.lineup = dlg.lineupId;
			_setupInfo.army = dlg.onLineupMan;
			_setupInfo.amount = dlg.amount;
			_setupInfo.type = SangoConst.ARMY_TYPE_ARMY;
		}
		public function updateUserCenter(x:int, y:int):void {
			var _vo:LocationVO = new LocationVO();
			_vo.x = x; _vo.y = y;
			sangoFacade.socket.sendSingleVo(RemoteMessage.REMOTE_UPDATE_USER_LOCATION, 0, _vo);
		}
	}
}
